﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace DataMonitor_Tools.Tools
{
    public static class ErrorMessage
    {
        /// <summary>
        /// 把error输出到tools_log.txt
        /// </summary>
        /// <param name="file"></param>
        /// <param name="ex"></param>
        public static void GetErrorMessage(FileInfo file, string message)
        {
            var content = "处理此文件名：" + file.Name + "发生了错误：" +message+ "\r\n";
            string path = Assembly.GetExecutingAssembly().Location;
            string? directory = Path.GetDirectoryName(path);

            if(Directory.Exists(directory))
            {
                // 获取当前日期
                string currentDate = DateTime.Now.ToString("yyyy-MM-dd");
                // 去掉破折号
                string formattedDate = currentDate.Replace("-", "");
                // 构建文件名
                string fileName = $"error_{formattedDate}.txt";
                string writepath = Path.Combine(directory, "dm_error");
                // 创建文件夹路径（如果不存在）
                Directory.CreateDirectory(writepath);
                // 指定文件路径
                string filePath = Path.Combine(writepath, fileName);
                File.AppendAllText(filePath, content);
                Console.WriteLine(directory);
            }

        }
        public static void GetErrorMessage(string file, string message)
        {
            var content = "处理此文件名：" + file + "发生了错误：" + message + "\r\n";
            string path = Assembly.GetExecutingAssembly().Location;
            string? directory = Path.GetDirectoryName(path);

            if (Directory.Exists(directory))
            {
                // 获取当前日期
                string currentDate = DateTime.Now.ToString("yyyy-MM-dd");
                // 去掉破折号
                string formattedDate = currentDate.Replace("-", "");
                // 构建文件名
                string fileName = $"error_{formattedDate}.txt";
                string writepath = Path.Combine(directory, "dm_error");
                // 创建文件夹路径（如果不存在）
                Directory.CreateDirectory(writepath);
                // 指定文件路径
                string filePath = Path.Combine(writepath, fileName);
                File.AppendAllText(filePath, content);
                Console.WriteLine(directory);
            }
        }
    }
}
